package com.glsc.ngateway.platform.controller.it;

import com.glsc.ngateway.common.api.common.enums.PageConstant;
import com.glsc.ngateway.common.api.platform.dto.it.ParamSearchF5MapDto;
import org.springframework.data.domain.Page;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.it.ItF5NetworkMap;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.platform.service.it.NetOpearationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/it/netOperation")
@Api(value = "netOperation管理", tags = {"netOperation管理"})
public class NetOperationController {
    private static final Logger logger = LoggerFactory.getLogger(NetOperationController.class);
    @Autowired
    private NetOpearationService netOpearationService;

    @RequestMapping(method = RequestMethod.POST, value = "truncateF5NetworkMap")
    @ApiOperation(value = "truncateF5NetworkMap", notes = "truncateF5NetworkMap")
    public PlatformResponse<?> truncateSwitchData() {

        netOpearationService.truncateF5NetworkMap();
        List<ItF5NetworkMap> checkTruncate = netOpearationService.findAllList();
        return checkTruncate.isEmpty() ? PlatformResponse.success() : PlatformResponse.failedMsg("清空失败");
    }

    @RequestMapping(method = RequestMethod.POST, value = "syncF5NetworkMap")
    @ApiOperation(value = "syncF5NetworkMap", notes = "syncF5NetworkMap")
    public PlatformResponse<?> syncF5NetworkMap(@RequestBody ItF5NetworkMap itF5NetworkMap) {

//        List<ItF5NetworkMap> aSwitch = netOpearationService.findSwitch(itF5NetworkMap);
//        if (aSwitch != null && aSwitch.size() > 0) {
//            PlatformResponseNew ret = PlatformResponseNew.failedMsg("添加失败，数据已存在。" + switchIp + " " + switchInterface + " " + macAddress);
//            return ret;
//        }
        netOpearationService.saveF5NetworkMap(itF5NetworkMap);
        return PlatformResponse.success();
    }

    @RequestMapping(method = RequestMethod.POST, value = "search")
    @ApiOperation(value = "searchF5NetworkMap", notes = "search")
    public PlatformResponse<Page<ItF5NetworkMap>> searchF5NetworkMap(@RequestBody ParamSearchF5MapDto params) {

        if (params.getPageNo() == null) {
            params.setPageNo(PageConstant.PageNo);
        }
        if (params.getPageSize() == null) {
            params.setPageSize(PageConstant.PageSize);
        }
        Page<ItF5NetworkMap> result = netOpearationService.searchF5NetworkMap(params.getKey(),params.getPageSize(),params.getPageNo());
        return PlatformResponse.successData(result);
    }

    @RequestMapping(value = "searchF5")
    @ApiOperation(value = "searchF5", notes = "searchF5")
    public PlatformResponse<List<ItF5NetworkMap>> searchF5(@RequestParam(value = "destination", required = false) String destination,@RequestParam(value = "port", required = false)  String port) {

        List<ItF5NetworkMap> result = netOpearationService.searchF5(destination,port);
        return PlatformResponse.successData(result);
    }

}
